﻿Imports System.Data.OleDb
Imports BUS
Imports DTO

Public Class frmLapPhieuThue

    Private ptBus As New PhieuThuePhongBUS()
    Private Sub frmLapPhieuThue_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LoadDuLieu()
        '-----------
        DuaDuLieuVaoComboBoxPhong()
        'DuaDuLieuVaoComboBoxKhachHang()
        Me.btnLuu.Visible = False
        Me.btnDongy.Visible = False
    End Sub
    Protected Function ConnectionData() As OleDbConnection
        Dim strCn As String
        strCn = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = QLKS.mdb"
        Dim cn As New OleDbConnection(strCn)
        cn.Open()
        Return cn
    End Function
    Private Sub DuaDuLieuVaoComboBoxPhong()
        ' nhap nhap chon csdl
        Dim cn As OleDbConnection
        cn = Me.ConnectionData()
        Dim strSQL As String
        strSQL = "select * from Phong"
        Dim da As New OleDbDataAdapter
        da = New OleDbDataAdapter(strSQL, cn)
        Dim ds As New DataSet
        da.Fill(ds, "Phong")
        With cboMaphong
            .DataSource = ds.Tables("Phong")
            .ValueMember = "MAPHONG"
            .DisplayMember = "TENPHONG"
        End With

        'Me.cboMapt.DataSource = ds.Tables(0).DefaultView

        ' Me.cboMapt.DisplayMember = "MAPHIEUTHUE"
        ' Me.cboMapt.ValueMember = "MAPHIEUTHUE"


    End Sub
    'Private Sub DuaDuLieuVaoComboBoxKhachHang()
    '    ' nhap nhap chon csdl
    '    Dim cn As OleDbConnection
    '    cn = Me.ConnectionData()
    '    Dim strSQL As String
    '    strSQL = "select * from khachhang"
    '    Dim da As New OleDbDataAdapter
    '    da = New OleDbDataAdapter(strSQL, cn)
    '    Dim ds As New DataSet
    '    da.Fill(ds, "KhachHang")
    '    With cboTenkh
    '        .DataSource = ds.Tables("KhachHang")
    '        .ValueMember = "MAKHACHHANG"
    '        .DisplayMember = "TENKH"
    '    End With

    '    'Me.cboMapt.DataSource = ds.Tables(0).DefaultView

    '    ' Me.cboMapt.DisplayMember = "MAPHIEUTHUE"
    '    ' Me.cboMapt.ValueMember = "MAPHIEUTHUE"


    'End Sub
    Private Sub LoadDuLieu()
        Dim cn As OleDbConnection
        cn = Me.ConnectionData()
        Dim strSQL As String
        strSQL = "select pt.MaPhieuThue, TenPhong, pt.MaPhong, NgayBatDauThue "
        strSQL += "from "
        strSQL += " PhieuThue pt, Phong p"
        strSQL += " where "
        strSQL += " pt.maphong = p.maphong"

        Dim da As OleDbDataAdapter
        da = New OleDbDataAdapter(strSQL, cn)
        Dim cmb As New OleDbCommandBuilder(da)

        Dim DataSet1 As New DataSet()
        da.Fill(DataSet1, "TestDB")
        dtgvDanhSachPhieuThue.DataSource = DataSet1.Tables("TestDB")

    End Sub



    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        Dim str As String
        str = MsgBox("Bắt đầu thêm mới dữ liệu - Mời bạn nhấp chuột nhập liệu", MsgBoxStyle.YesNo, "Chú ý! chú ý!")
        If str = "6" Then

            Me.txtMapt.Enabled = False
            Me.btnLuu.Visible = True
            Me.btnThem.Visible = False

        End If
    End Sub

    Private Sub dtgvDanhSachPhieuThue_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtgvDanhSachPhieuThue.SelectionChanged
        Try
            Me.txtMapt.Text = Me.dtgvDanhSachPhieuThue.CurrentRow.Cells(0).Value
            '  Me.cboTenkh.SelectedValue = Me.dtgvDanhSachPhieuThue.CurrentRow.Cells(1).Value
            Me.txtNgaylap.Text = Me.dtgvDanhSachPhieuThue.CurrentRow.Cells(3).Value.ToString
            Me.cboMaphong.SelectedValue = Me.dtgvDanhSachPhieuThue.CurrentRow.Cells(2).Value

        Catch ex As Exception

        End Try
    End Sub
 
    Private Sub btnLuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLuu.Click
        Try
            Dim dt As New DataTable
            dt = ptBus.LayBang()
            Dim dr As DataRow
            dr = dt.NewRow()
            dr("NgayBatDauThue") = txtNgaylap.Text
            dr("MaPhong") = Me.cboMaphong.SelectedValue
            
            ptBus.ThemPhieuThueBUS(dr)
            MessageBox.Show("Thêm thành công", "Thông báo !", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Me.txtMapt.Enabled = True
            Me.btnLuu.Visible = False
            Me.btnThem.Visible = True
            LoadDuLieu()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        Dim str As String
        str = MsgBox("Xóa dữ liệu ?", MsgBoxStyle.YesNo, "Chú ý! chú ý!")
        If str = "6" Then
            ptBus.XoaPhieuThueBUS(txtMapt.Text)
            MessageBox.Show("Xóa thành công", "Thông báo !", MessageBoxButtons.OK, MessageBoxIcon.Information)
            LoadDuLieu()
        End If
    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        Dim str As String
        str = MsgBox("Sửa dữ liệu?", MsgBoxStyle.YesNo, "Chú ý! chú ý!")
        If str = "6" Then

            Me.txtMapt.Enabled = False
            Me.btnDongy.Visible = True
            Me.btnSua.Visible = False

        End If
    End Sub

    Private Sub btnDongy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDongy.Click
        Try
            Dim dt As New DataTable
            dt = ptBus.LayBang()
            Dim dr As DataRow
            dr = dt.NewRow()
            dr("MaPhieuThue") = txtMapt.Text
            dr("NgayBatDauThue") = txtNgaylap.Text
            dr("MaPhong") = Me.cboMaphong.SelectedValue
         

            ptBus.SuaPhieuThueBUS(dr)
            Me.txtMapt.Enabled = True
            Me.btnDongy.Visible = False
            Me.btnSua.Visible = True
            MessageBox.Show("Sửa thành công", "Thông báo !", MessageBoxButtons.OK, MessageBoxIcon.Information)
            LoadDuLieu()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub btnLapctpt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLapctpt.Click
        Dim frm As New frmChiTietPhieuThue
        frm.ShowDialog()
    End Sub
End Class